// miniprogram/pages/skillPractice/skillPractice.js
import {
    $wuxDialog
} from '../../dist-wux/index'
import {
    $wuxSelect
} from "../../dist-wux/index";

var app = getApp()
var httpUtils = require('../../utils/httpUtils')
var api = require("../../utils/API")
var utils = require("../../utils/utils")
var commonFields = require("../../utils/commonFields")
var sliderWidth = 96;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: utils.getStorageSync(commonFields.userInfo),
        wxUserInfo: utils.getStorageSync(commonFields.wxUserInfo),
        banners: httpUtils.BASE_URL + "/Content/image/main_logo.jpg",
        activeIndex: 0,
        current: "0",
        subjects: [{
            key: "0",
            title: "科目一",
            content: [{
                icon: "../../images/picture.png",
                name: "图片基础知识",
                url: api.PAGES_PICTURE_BASICS,
                questionType: "picture",
                subject: 1
            },
            {
                icon: "../../images/judge.png",
                name: "判断题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "judge",
                subject: 1
            },
            {
                icon: "../../images/single-choice.png",
                name: "单选题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "single-choice",
                subject: 1
            },
            {
                icon: "../../images/technique.png",
                name: "答题技巧",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "technique",
                subject: 1
            },
            {
                icon: "../../images/simplify.png",
                name: "精简题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "simplify",
                subject: 1
            },
            {
                icon: "../../images/normal.png",
                name: "常规练习",
                url: api.PAGES_NORMAL_EXERCISE,
                questionType: "normal",
                subject: 1
            }
            ]
        },
        {
            key: "4",
            title: "科目四",
            content: [{
                icon: "../../images/multi-choice.png",
                name: "多选题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "multi-choice",
                subject: 4
            },
            {
                icon: "../../images/judge.png",
                name: "判断题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "judge",
                subject: 4
            },
            {
                icon: "../../images/single-choice.png",
                name: "单选题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "single-choice",
                subject: 4
            },
            {
                icon: "../../images/technique.png",
                name: "答题技巧",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "technique",
                subject: 4
            },
            {
                icon: "../../images/simplify.png",
                name: "精简题",
                url: api.PAGES_GENERAL_EXERCISE,
                questionType: "simplify",
                subject: 4
            },
            {
                icon: "../../images/normal.png",
                name: "常规练习",
                url: api.PAGES_NORMAL_EXERCISE,
                questionType: "normal",
                subject: 4
            },
            ]
        },
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let userInfo = utils.getStorageSync(commonFields.userInfo)
        if (userInfo) {
            this.setData({
                userInfo: userInfo,
                banners: userInfo.isvip || utils.getStorageSync(commonFields.isIOS) ? httpUtils.BASE_URL + "/Content/image/main_logo.jpg" : httpUtils.BASE_URL + "/Content/image/vip.gif",
                bannersClick: !userInfo.isvip && !utils.getStorageSync(commonFields.isIOS),
            })
        } else {
            this.setData({
                userInfo: null,
                banners: utils.getStorageSync(commonFields.isIOS) ? httpUtils.BASE_URL + "/Content/image/main_logo.jpg" : httpUtils.BASE_URL + "/Content/image/vip.gif",
                bannersClick: utils.getStorageSync(commonFields.isIOS) ? false : true,
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    handleChange({
        detail
    }) {
        this.setData({
            activeIndex: detail.key,
            current: detail.key
        });
    },

    onTabsChange(e) {
        console.log('onTabsChange', e)
        const {
            key
        } = e.detail
        const index = this.data.subjects.map((n) => n.key).indexOf(key)

        this.setData({
            key,
            index,
        })
    },

    onSwiperChange(e) {
        console.log('onSwiperChange', e)
        const {
            current: index,
            source
        } = e.detail
        const {
            key
        } = this.data.subjects[index]

        if (!!source) {
            this.setData({
                key,
                index,
            })
        }
    },
    onClickGridItem(e) {
        let subject = e.currentTarget.dataset.item.subject;
        let questionType = e.currentTarget.dataset.item.questionType;
        if (questionType != 'normal') {
            var userInfo = utils.getStorageSync(commonFields.userInfo);
            if (userInfo) {
                if (userInfo.isvip) {
                    wx.navigateTo({
                        url: e.currentTarget.dataset.item.url + "?subject=" + subject + "&questionType=" + questionType
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: utils.getStorageSync(commonFields.isIOS) ? '请联系客服' : '请先开通会员',
                        showCancel: false,
                        success(res) {

                        }
                    })
                }
            } else {
                wx.showModal({
                    title: '提示',
                    content: '请先登录',
                    showCancel: false,
                    success(res) {

                    }
                })
            }
        } else {
            wx.navigateTo({
                url: e.currentTarget.dataset.item.url + "?subject=" + subject + "&questionType=" + questionType
            })
        }
    },
    /**
     * 去 我的 页面
     * @param e
     */
    naviTo(e) {
        let userInfo = utils.getStorageSync(commonFields.userInfo)
        let isIOS = utils.getStorageSync(commonFields.isIOS)
        if (userInfo && !isIOS) {
            wx.navigateTo({
                url: api.PAGES_BUYVIP_BUYVIP
            })
            return
        }
        if (isIOS) {
            utils.setStorageSync(commonFields.isOpenBuyVipPage, false)
        } else {
            utils.setStorageSync(commonFields.isOpenBuyVipPage, true)
        }
        wx.switchTab({
            url: api.PAGES_MINE_MINE
        })
    },
});